package com.orcamento.dao;

import java.util.List;

import org.joda.time.DateTime;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.orcamento.entity.Exchange;
import com.orcamento.entity.enums.Month;
import com.orcamento.util.CalendarUtil;

public class ExchangeDao extends AbstractDao<Exchange> {

	public ExchangeDao() {
		super(Exchange.class);
	}

	@Transactional(propagation=Propagation.REQUIRED)
	public Exchange save(Exchange exchange) {
		return saveEntity( exchange );
	}

	public List<Exchange> findAllByAccount(Long accountId) {
		return super.findAllByAccount(accountId);
	}

	public List<Exchange> findByMonth(Month month, Integer year, Long accountId) {
		DateTime startDate = CalendarUtil.toDate(1, month.id, year, 0, 0, 0);
		DateTime endDate = CalendarUtil.toDate(month.maxDays, month.id, year, 23, 59, 59); 
		return findBetweenByAccount("date", startDate, endDate, accountId);
	}

	public Exchange findByIdAndAccount(Long id, Long accountId) {
		return super.findByIdAndAccount(id, accountId);
	}

	public List<Exchange> findBetweenByAccount(DateTime startDate, DateTime endDate, Long accountId) {
		return super.findBetweenByAccount("date", startDate, endDate, accountId);
	}

}
